package com.duke.shaking.utils.pay;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import com.duke.shaking.utils.MD5Util;
import com.duke.shaking.utils.pay.ProductDetail;
import com.sohu.pay.order.OrderInfo;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;

public class PayUtils {

	public static final String channelId = "1";
	
	public static String getProductId(Context context) {
		ApplicationInfo appInfo = null;
		try {
			appInfo = context.getPackageManager().getApplicationInfo(
					context.getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if (appInfo != null) {
			Bundle bundle = appInfo.metaData;
			if (bundle != null) {
				String appId = bundle.getInt("sohu_vgo_appkey") + "";
				if (appId == null || "".equals(appId)) {
					int iappId = bundle.getInt("sohu_vgo_appkey");
					if (iappId != 0) {
						appId = String.valueOf(iappId);
					}
				}
				if (appId != null && appId.length() > 0) {
					return appId;
				}
			}
		}
		return "";
	}

	public static String getMerchantId(Context context) {
		ApplicationInfo appInfo = null;
		try {
			appInfo = context.getPackageManager().getApplicationInfo(
					context.getPackageName(), PackageManager.GET_META_DATA);
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
		if (appInfo != null) {
			Bundle bundle = appInfo.metaData;
			if (bundle != null) {
				String userId = bundle.getInt("sohu_vgo_userkey") + "";
				if (userId == null || "".equals(userId)) {
					int iuseId = bundle.getInt("sohu_vgo_userkey");
					if (iuseId != 0) {
						userId = String.valueOf(iuseId);
					}
				}
				if (userId != null && userId.length() > 0) {
					return userId;
				}
			}
		}
		return null;
	}

	public static String getChannelId() {
		return channelId;
	}
	
	private static String generateSign(OrderInfo orderInfo, String signKey) {
		StringBuilder sb = new StringBuilder();
		String cpid = orderInfo.getSmid();

		sb.append("smid=");
		sb.append(cpid);

		String cpcid = orderInfo.getCid();
		sb.append("&cid=");
		sb.append(cpcid);

		String cppid = orderInfo.getPid();
		sb.append("&pid=");
		sb.append(cppid);

		String payID = orderInfo.getPayID();
		sb.append("&payID=");
		sb.append(payID);

		String orderno = orderInfo.getOrderno();

		sb.append("&orderno=");
		sb.append(orderno);

		String ordertime = orderInfo.getOrdertime();
		sb.append("&ordertime=");
		sb.append(ordertime);

		try {
			String name = orderInfo.getName();
			sb.append("&name=");
			if (name != null && name.length() > 0) {
				sb.append(URLDecoder.decode(name, "utf-8"));
			}

			String namedesc = orderInfo.getNamedesc();
			sb.append("&namedesc=");
			if (namedesc != null && namedesc.length() > 0) {
				sb.append(URLDecoder.decode(namedesc, "utf-8"));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		sb.append("&");
		sb.append(signKey);
		// sb.append("sdfsdfsdf");

		String mysign = MD5Util.getMD5String(sb.toString());

		return mysign;
	}
	
	public static OrderInfo createOrderInfo(int productType, 
			String orderId, 
			String orderTime,
			String sign,
			Context context) {
		OrderInfo orderInfo = new OrderInfo();
		orderInfo.setCid(channelId);
		ProductDetail detail = Products.getProductDetail(productType);
		
		orderInfo.setName(detail.payName);
		orderInfo.setNamedesc(detail.body);
		orderInfo.setOrderno(orderId);
		/*SimpleDateFormat format = new SimpleDateFormat(
				"yyyy-MM-dd hh:mm:ss", Locale.CHINA);
		Date now = new Date();
		orderInfo.setOrdertime(format.format(now));*/
		orderInfo.setOrdertime(orderTime);
		orderInfo.setPayID(detail.payID);
		// orderInfo.setPid("1");
		// orderInfo.setSmid("10001");
		orderInfo.setPid(getProductId(context));
		orderInfo.setSmid(getMerchantId(context));
		
		//String sign = generateSign(orderInfo, signKey);
		orderInfo.setSign(sign);
		return orderInfo;
	}
}
